﻿using System.Collections.Generic;
using Shell.DomainDot;
using Framework.Poco;
using System;
using System.Linq;

namespace Framework.Domain
{
    /// <summary>
    /// 经销商图片分类
    /// </summary>
    public class PictureCategoryContext : DomainDotBase<Agency>
    {
        /// <summary>
        /// 与经销商相关的经销商图片分类
        /// </summary>
        public WithMany<PictureCategory> AgencyPictureCategories { private get; set; }

        /// <summary>
        /// 获取属于经销商的图片分类
        /// </summary>
        public IEnumerable<PictureCategory> GetAllAgencyPictureCategory()
        {
            return this.AgencyPictureCategories;
        }

        /// <summary>
        /// 根据一个ID获得一个图片分类
        /// </summary>
        public PictureCategory GetPictureCategory(Guid identity)
        {
            return this.AgencyPictureCategories.SingleOrDefault(p => p.Id.Equals(identity));
        }

        /// <summary>
        /// 排序后的的经销商图片分类
        /// </summary>
        public IEnumerable<PictureCategory> Select(int currentPage, int pageSize)
        {
            var total = this.AgencyPictureCategories.Count;
            return this.AgencyPictureCategories.Skip((currentPage - 1) * pageSize).Take(pageSize);
        }

        /// <summary>
        /// 创建经销商图片分类
        /// </summary>
        public void Create(PictureCategory instance)
        {
            instance.Created = DateTime.Now;
            var obj = this.AgencyPictureCategories.Create();
            PocoUtils.AssignProperty(instance, obj, typeof(Guid));
            this.AgencyPictureCategories.Submit();
        }

        /// <summary>
        /// 删除经销商图片分类
        /// </summary>
        public void Delete(Guid identity)
        {
            var original = this.GetAllAgencyPictureCategory().SingleOrDefault(o => o.Id.Equals(identity));
            if (original != null)
            {
                this.AgencyPictureCategories.Delete(original.Id);
                this.AgencyPictureCategories.Submit();
            }
        }

        /// <summary>
        /// 修改经销商图片分类
        /// </summary>
        public void Edit(PictureCategory instance)
        {
            var original = this.GetAllAgencyPictureCategory().SingleOrDefault(o => o.Id.Equals(instance.Id));
            if (original != null)
            {
                original.Name = instance.Name;
                original.Rank = instance.Rank;
                this.AgencyPictureCategories.Submit();
            }
        }
    }
}
